package b4.scene.town
{
	import as3isolib.core.*;
	import as3isolib.display.*;
	import as3isolib.display.primitive.*;
	import as3isolib.display.renderers.*;
	import as3isolib.display.scene.*;
	import as3isolib.geom.*;
	import as3isolib.graphics.*;
	
	import b4.model.basic.*;
	import b4.model.config.*;
	import b4.model.town.*;
	import b4.net.*;
	import b4.scene.town.building.*;
	import b4.util.profile.*;
	
	import com.greensock.*;
	import com.greensock.easing.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;

	public class Town extends Sprite
	{
		public var isoScene:IsoScene;
		public var isoView:IsoView;

		public static const gridWidth:int = 100;
		private var lockGrids:IsoSprite;
		
		private var mouseDownPos:Pt;
		private var currentViewCenter:Pt;
		private var mouseDownViewCenter:Pt;
		
		private var currentBuildingIndex:int;
		
		private var townModel:TownModel;
		
		public function Town(townModel:TownModel)
		{
			super();
			this.townModel = townModel;
			//场景               
			isoScene = new IsoScene();                        
			isoScene.hostContainer = this;
			//坐标
//			var coordinate:CoordinateGrid = new CoordinateGrid();
//			coordinate.cellSize = gridWidth;
////			coordinate.setGridSize(60, 60, 2.2);
//			coordinate.showOrigin = true;
//			isoScene.addChild(coordinate);
			//背景
			var LayerBgr:Class = ResourceManager.getInstance().getClassByName("TownBgr1");
			var bgGrid:IsoSprite = new IsoSprite();
			bgGrid.setSize(gridWidth, gridWidth, 0.1);
			bgGrid.moveBy(0, 0, -2);
			isoScene.addChild(bgGrid);
			bgGrid.sprites = [LayerBgr]
			bgGrid.container.mouseEnabled = false;
			bgGrid.container.mouseChildren = false;
			//阴影
			var factory:as3isolib.core.ClassFactory = new as3isolib.core.ClassFactory(DefaultShadowRenderer);
			factory.properties = {shadowColor:0x000000, shadowAlpha:0.15, drawAll:false};                        
			isoScene.styleRenderers = [factory];                                                
			isoScene.render();
			//设置视角   
			isoView = new IsoView();                        
			isoView.setSize(GameProfile.stageWidth, GameProfile.stageHeight);
			isoView.addScene(isoScene);
			isoView.centerOnPt(new Pt(460, 350));
			addChild(isoView);
			
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}
		
		private function onAddedToStage(event:Event):void
		{
			townModel.addEventListener(ModelEvent.TOWN_REFRESH, onTownRefresh, false, 0, true);
		}
		
		private function onRemovedFromStage(event:Event):void
		{
			townModel.removeEventListener(ModelEvent.TOWN_REFRESH, onTownRefresh);
		}
		
		private function onTownRefresh(event:ModelEvent):void
		{
			clearBuildings();
			var buildingList:Object = townModel.buildingList;
			for (var idStr:String in buildingList)
			{
				var buildingModel:BuildingModel = buildingList[idStr];
				var buildingConfig:Object = GameConfig.basicConfig.buildingConfigs[buildingModel.buildingIndex];
				var building:Building = BuildingFactory.createBuilding(buildingModel);
				building.moveTo(buildingConfig.posX, buildingConfig.posY, 0.1);
				isoScene.addChild(building);
			}
			isoScene.render();
		}
				
		//清除所有建筑
		public function clearBuildings():void
		{
			for (var i:int = 0,len:int = isoScene.children.length; i<len; i++)
			{
				if (isoScene.children[i] is Building)
				{
					var building:Building = isoScene.children[i]; 
					isoScene.removeChild(building);
					building.destroy();
					i--;
					len--;
				}
			}
		}
		
		public function destroy():void
		{
			clearBuildings();
		}
	}
}